package com.shanyu.nicefish.controller;

import com.shanyu.nicefish.common.Message;
import com.shanyu.nicefish.common.MessageUtil;
import com.shanyu.nicefish.event.OrderEvent;
import com.shanyu.nicefish.service.HrService;
import com.shanyu.nicefish.service.StrategyContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 管理员
 *
 * @blame czj Team
 */
@RestController
@Api(tags = "管理者用户相关接口")
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private HrService hrservice;

    @PostMapping("/login")
    @ApiOperation(value = "登入身份验证（JWT验证）", notes = "登入")
    public String login(String username, String password) {
        // TODO 这里面不需要写任何代码，由UserDeatilsService去处理
        return "hello";
    }

    /**
     * 加载 事件发布类
     */
    @Autowired
    ApplicationContext applicationContext;

    /**
     * 策略上下文
     */
    @Autowired
    private StrategyContext strategyContext;

    @PostMapping("/")
    @ApiOperation("添加用户的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    })
    public Response addUser(String username, @RequestParam(required = true) String address) {
        return new Response();
    }


    @GetMapping("/{userId}")
    @ApiOperation("根据id更新用户的接口")
    public void getUserById(@PathVariable int userId) {
        if (userId > 0) {
            applicationContext.publishEvent(new OrderEvent(applicationContext, Integer.toString(userId)));
        }
    }

    @GetMapping("/getMemberInfo")
    @ApiOperation("根据id更新用户的接口")
    public BigDecimal getMemberPrice() {
        return strategyContext.calcPrice("NORMAL");
    }

    @GetMapping("/getPass")
    @ApiOperation("根据id更新用户的接口")
    public Message<String> getPass() {
        String pass = "1234";
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(pass);
        System.out.println(hashPass);
        return MessageUtil.success(hashPass);
    }



}
